In [1]:
%matplotlib inline
import numpy as np
import tensorflow as tf
import matplotlib.pyplot as plt
In [2]:
## launching a tensorflow session
sess = tf.InteractiveSession()
In [3]:
a = tf.constant(1)
a
Out[3]:
In [4]:
sess.run(a)
Out[4]:
In [5]:
a = tf.constant([1.,2.,3.])
sess.run(a)
Out[5]:
In [6]:
np_a = np.random.rand(2,2,3,4)
print np_a.shape
a = tf.constant(np_a)
sess.run(a)
Out[6]:
In [7]:
a.get_shape()
Out[7]:
In [8]:
tf_const = tf.constant(200)
tf_var = tf.Variable(100,name="tf_variable")
add_vars = tf.add(tf_var, tf_const)
update = tf.assign(tf_var, add_vars)
init_ops = tf.initialize_all_variables()
sess.run(init_ops)
print "tf_var: %s" % sess.run(tf_var)
print "tf_const: %s" % sess.run(tf_const)
print "add_vars: %s" % sess.run(add_vars)
for i in xrange(5):
print "Update: %s, tf_var: %s" % (sess.run(update),
sess.run(tf_var))
In [9]:
sess.close()
In [10]:
from tensorflow.examples.tutorials.mnist import input_data
mnist = input_data.read_data_sets('MNIST_data', one_hot=True)
In [11]:
sess = tf.InteractiveSession()
In [12]:
x = tf.placeholder(tf.float32, shape=[None, 784])
y_ = tf.placeholder(tf.float32, shape=[None, 10])
In [13]:
W = tf.Variable(tf.zeros([784,10]))
b = tf.Variable(tf.zeros([10]))
In [14]:
y = tf.matmul(x,W) + b
In [15]:
cross_entropy = tf.reduce_mean(tf.nn.softmax_cross_entropy_with_logits(y,y_))
train_step = tf.train.GradientDescentOptimizer(0.5).minimize(cross_entropy)
In [16]:
correct_prediction = tf.equal(tf.argmax(y,1), tf.argmax(y_,1))
accuracy = tf.reduce_mean(tf.cast(correct_prediction, tf.float32))
In [17]:
init_ops = tf.initialize_all_variables()
sess.run(init_ops)
In [18]:
%%time
train_losses = []
test_losses = []
train_accuracy = []
test_accuracy = []
for i in xrange(1001):
batch = mnist.train.next_batch(100)
step, loss = sess.run([train_step, cross_entropy], feed_dict={x:batch[0], y_:batch[1]})
train_losses.append(loss)
loss, acc = sess.run([cross_entropy, accuracy], feed_dict={x: mnist.test.images, y_: mnist.test.labels})
test_losses.append(loss)
test_accuracy.append(acc)
acc = sess.run(accuracy, feed_dict={x: mnist.train.images, y_: mnist.train.labels})
train_accuracy.append(acc)
if i%100 == 0:
print "[%s] Loss: %.3f (Train) %.3f (Test); Accuracy: %.3f (Train) %.3f (Test)" % (
i, train_losses[-1], test_losses[-1], train_accuracy[-1], test_accuracy[-1]
)
In [19]:
fig, ax = plt.subplots(2,1, sharex=True, figsize=(10,10))
ax[0].plot(train_losses, "-b", label="train")
ax[0].plot(test_losses, "-r", label="test")
ax[0].set_xlabel("iteration")
ax[0].set_ylabel("loss")
ax[0].legend()
ax[1].plot(train_accuracy, "-b", label="train")
ax[1].plot(test_accuracy, "-r", label="test")
ax[1].set_xlabel("iteration")
ax[1].set_ylabel("accuracy")
ax[1].legend()
fig.tight_layout()
In [20]:
sess.close()
sess = tf.InteractiveSession()
In [21]:
x = tf.placeholder(tf.float32, shape=[None, 784])
y_ = tf.placeholder(tf.float32, shape=[None, 10])
In [22]:
def weight_initialization(shape):
return tf.Variable(tf.truncated_normal(shape, stddev=0.5))
def bias_initialization(shape):
return tf.Variable(tf.constant(0.1, shape=shape))
def conv2d(x,W, strides=[1,1,1,1], padding="SAME"):
return tf.nn.conv2d(x,W,strides=strides, padding=padding)
def max_pool(x, ksize=[1,2,2,1], strides=[1, 2, 2, 1], padding='SAME'):
return tf.nn.max_pool(x, ksize=ksize, strides=strides, padding=padding)
In [23]:
x_image = tf.reshape(x,[-1,28,28,1])
# 1st Convolution + max pooling
W_conv1 = weight_initialization([5,5,1,32])
b_conv1 = bias_initialization([32])
h_conv1 = tf.nn.relu(conv2d(x_image,W_conv1)+b_conv1)
h_pool1 = max_pool(h_conv1)
# 2nd Convolution + max pooling
W_conv2 = weight_initialization([5,5,32,64])
b_conv2 = bias_initialization([64])
h_conv2 = tf.nn.relu(conv2d(h_pool1,W_conv2)+b_conv2)
h_pool2 = max_pool(h_conv2)
# 1st fully connected
W_fc1 = weight_initialization([7*7*64, 1024])
b_fc1 = bias_initialization([1024])
h_pool2_flat = tf.reshape(h_pool2, [-1, 7*7*64])
h_fc1 = tf.matmul(h_pool2_flat, W_fc1) + b_fc1
# Dropout
keep_prob = tf.placeholder(tf.float32)
h_fc1_drop = tf.nn.dropout(h_fc1, keep_prob)
# Output
W_fc2 = weight_initialization([1024, 10])
b_fc2 = bias_initialization([10])
y_conv = tf.matmul(h_fc1_drop, W_fc2) + b_fc2
In [24]:
cross_entropy = tf.reduce_mean(tf.nn.softmax_cross_entropy_with_logits(y_conv, y_))
train_step = tf.train.AdamOptimizer(1e-4).minimize(cross_entropy)
correct_prediction = tf.equal(tf.argmax(y_conv, 1), tf.argmax(y_, 1))
accuracy = tf.reduce_mean(tf.cast(correct_prediction, tf.float32))
init_op = tf.initialize_all_variables()
In [25]:
sess.run(init_op)
In [26]:
%%time
train_losses = []
test_losses = []
train_accuracy = []
test_accuracy = []
for i in xrange(20001):
batch = mnist.train.next_batch(30)
step, loss = sess.run([train_step, cross_entropy], feed_dict={x:batch[0], y_:batch[1], keep_prob: 0.5})
if i%500 == 0:
train_losses.append(loss)
acc = sess.run(accuracy, feed_dict={x: mnist.train.images, y_: mnist.train.labels, keep_prob: 1.})
train_accuracy.append(acc)
loss, acc = sess.run([cross_entropy, accuracy], feed_dict={x: mnist.test.images, y_: mnist.test.labels, keep_prob: 1.})
test_losses.append(loss)
test_accuracy.append(acc)
print "[%s] Loss: %.3f (Train) %.3f (Test); Accuracy: %.3f (Train) %.3f (Test)" % (
i, train_losses[-1], test_losses[-1], train_accuracy[-1], test_accuracy[-1]
)
In [29]:
fig, ax = plt.subplots(2,1, sharex=True, figsize=(10,10))
ax[0].plot(train_losses, "-b", label="train")
ax[0].plot(test_losses, "-r", label="test")
ax[0].set_xlabel("iteration")
ax[0].set_ylabel("loss")
ax[0].set_yscale("symlog")
ax[0].legend()
ax[1].plot(train_accuracy, "-b", label="train")
ax[1].plot(test_accuracy, "-r", label="test")
ax[1].set_xlabel("iteration")
ax[1].set_ylabel("accuracy")
ax[1].legend()
fig.tight_layout()
In [53]:
batch = mnist.test.next_batch(5)
x_img_true, y_conv_pred = sess.run([x_image, y_conv], feed_dict={x: batch[0], y_: batch[1], keep_prob: 1.})
def plot_digit(x,y,y_pred,ax, axhist=None):
x = np.reshape(x, (28,28))
ax.imshow(x, cmap="Greys")
ax.set_title("y=%s, y_pred=%s" % (np.argmax(y), np.argmax(y_pred)))
ax.axis('off')
if axhist is not None:
y_pred = sess.run(tf.nn.softmax(y_pred))
#axhist.plot(y_pred, "-bo")
axhist.bar(np.arange(10), y_pred, 0.5, color='r')
fig, ax = plt.subplots(2,5, figsize=(20,4))
for xx, yy, yy_pred, axi, axihist in zip(x_img_true, batch[1],
y_conv_pred, ax[0], ax[1]):
plot_digit(xx,yy,yy_pred,axi, axihist)
fig.tight_layout()
In [ ]: